package server;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import common.Level;
import common.Player;
import common.PlayerColor;


public class PlayerFactory {

	private Level level;
	private Player[] playerSlot = new Player[GameController.MAX_PLAYERS];
	
	public PlayerFactory() {
		for( Player p : playerSlot){
			p = null;
		}
	}
	
	public PlayerFactory( Level level){
		this();
		setLevel(level);		
	}
	
	public UpdateablePlayer getNextPlayer(String playerName) {
		int slot = 0;
		while( playerSlot[slot] != null){
			slot++;
		}
		if( slot >= GameController.MAX_PLAYERS){
			return null;
		}
		
		int[] pos = level.getStartPosition(slot);
		UpdateablePlayer p = new UpdateablePlayerImpl(pos[0], pos[1]);
		p.SetColor( level.getStartColor(slot));

		if (playerName == null || playerName.equals(""))
			p.SetName("Player " + slot);
		else
			p.SetName(playerName);

		playerSlot[slot] = p;
		return p;
	}
	
	public boolean hasNext() {
		for( Player p : playerSlot){
			if( p == null){
				return true;
			}
		}
		return false;
	}
	
	public void setLevel(Level l) {
		level = l;
	}
	
	public void releasePlayer(Player p) {
		for( int i = 0; i < 3; i++){
			if( playerSlot[i] == p){
				playerSlot[i] = null;
			}
		}
	}
	
}